<?php

include_once 'Db.class.php';

class Login extends Db{
    
    private $login;
    
    private $pass;
            
    function __construct($login, $pass) {
        
        parent::__construct();
        
        $this->login = $login;
        
        $this->pass = $pass;
        
    }
    
    public function validaLogin(){
        
        $this->query = "SELECT COUNT(*) AS cantidad
                        FROM usuarios
                        WHERE UPPER(login) = UPPER('$this->login')
                        AND password = '$this->pass'";
        
        echo $this->query;
        
        $this->retornaResultado(1);
        
        $this->liberaResult();
        
        return $this->row;
        
    }
    
    private function obtieneDatos(){
        
        $this->query = "SELECT id_usuario, CONCAT(nombre, ' ', apellido) AS nombre_usuario,
                        login, email
                        FROM usuarios
                        WHERE UPPER(login) = UPPER('$this->login')";
        
        $this->retornaResultado(2);
        
        $this->liberaResult();
        
        return $this->row;
        
    }
    
    public function creaSesion(){
        
        $this->obtieneDatos();
        
        session_start();
        
        $_SESSION['autenticado'] = 1;
    
        $_SESSION['nombre_usuario'] = $this->row[0]['nombre_usuario'];

        $_SESSION['usuario'] = $this->row[0]['login'];

        $_SESSION['id_usuario'] = $this->row[0]['id_usuario'];

        $_SESSION['email'] = $this->row[0]['email'];
        
    }
    
}

?>
